<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2016-2026 欣才开裆裤开发组
 * 网站地址: http://git.oschina.net/ts128
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 。
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 人码
 * Date: 2016-10-15
 */
namespace Admin\Controller;
use Admin\Controller\BaseController;
class SenicController extends BaseController {
    private $perpage = 10;
    private $M;
    public function _initialize(){
        $this->M = M("Senic");
    }
/*
*   分页
*/
    public function index(){
        $count = $this->M->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $this->M->alias("s")->join(array(
            "LEFT JOIN y_theme t on s.theme_id=t.id"
        ))->field(array(
            "s.*",
            "t.value"=> "value"
        ));
        $list = $this->M->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        session_start();
        $id=I("get.");
        $id=$id["id"];
        $_SESSION["pid"]=$id;
        $id=$_SESSION["pid"];
        $M=M("admin_node");
        $node=$M->where("parent_id='$id'")->select();
        $_SESSION["node"]=$node;
        $this->display();
    }
/**
*   添加/修改景点信息
*/
    public function edit(){
        $id = I("id");
        if(!empty($id)){
            $data = $this->M->where("id=".$id)->select();
            $this->assign("data", $data[0]);
        }
        $theme = M("theme")->select();
        $this->assign("theme", $theme);
        $this->display();
    }
/**
*   添加/修改景点信息处理过程
*/
    public function doEdit(){
        $data = $this->M->create();
        if(empty($data["id"])){
            $res = $this->M->data($data)->add();
            // 初次添加数据时上传图片
            $picres = $this->upPhoto($res);
        }
        else{
            $res = $this->M->data($data)->save();
            // 更新信息时上传图片
            $picres = $this->upPhoto($data["id"]);
        }
        if($res || $picres){
            $this->success("景点信息已被修改，".$picres, U("Senic/index"));
        }
        else{
            $this->error("操作失败", U("Senic/index"));
        }
    }
/*
*   景点图片上传
*/
    public function upPhoto($id = ""){
        if(empty($id)){
            return ;
        }
        // 图片上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =     CONTROLLER_NAME.'/'; // 设置附件上传目录    // 上传文件
        $upload->subName = array('date','Y/m');
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            return $upload->getError();
        }else{// 上传成功
            $pic = M("senic_pic");
            $picture = array();
            foreach($info as $v){
                $picture = substr($v["savepath"], strlen(CONTROLLER_NAME)+1) . $v["savename"];
                $pic->data(array("senic_id"=>$id, "picture"=>$picture))->add();
            }
            return count($info)."个图片已上传";
        }
    }
/*
*   ajax请求查询景点图片
*/
    public function getPic(){
        $id = I("id");
        $picinfo["pic"] = M("senic_pic")->where("senic_id=".$id)->select();
        $picinfo["pics"] = M("senic_pic")->where("senic_id=".$id)->count();
        echo json_encode($picinfo);
    }
/*
*   ajax请求删除景点图片
*/
    public function delPic(){
        $id = I("id");
        $picture = $this->M->table("__SENIC_PIC__")->where("id=".$id)->select();
        $delres = unlink(C("TMPL_PARSE_STRING")["__UPLOAD__"]."/".CONTROLLER_NAME."/".$picture[0]["picture"]);
        if($delres){
            $res = $this->M->table("__SENIC_PIC__")->where("id=".$id)->delete();
        }
        echo $res;
    }
/**
*   删除景点信息
*/
    public function del(){
        $id = I("id");
        $pic = M("senic_pic");
        $res1 = $this->M->where("id=".$id)->delete();
        $data = $pic->where("senic_id=".$id)->select();
        foreach($data as $v){
            unlink(C("TMPL_PARSE_STRING")["__UPLOAD__"]."/".CONTROLLER_NAME."/".$v["picture"]);
        }
        $res2 = $pic->where("senic_id=".$id)->delete();
        if($res1){
            $this->success("操作成功", U("Senic/index"));
        }
        else{
            $this->error("操作失败", U("Senic/index"));
        }
    }

// ----------------分--------割--------线--------小--------盆--------友----------------

/*
*   主题列表
*/
    public function theme(){
        $count = M("theme")->count();    // 总记录数
        $pages = ceil($count / $this->perpage);
        if(empty($nowpage)){
            $nowpage = 1;
        }
        $Page = new \Think\Page($count, $this->perpage);
        $Page->setConfig("prev", "上一页");
        $Page->setConfig("next", "下一页");
        $this->M->alias("s")->join(array(
            "LEFT JOIN y_theme t on s.theme_id=t.id"
        ))->field(array(
            "s.*",
            "t.value"=> "value"
        ));
        $list = M("theme")->limit($Page->firstRow.','.$Page->listRows)->select();   // 每页的数据数组
        $show = $Page->show();  // 页码链接内容
        $this->assign("count", $count);
        $this->assign("list", $list);
        $this->assign("page", $show);
        $this->assign("pages", $pages);
        $this->assign("nowpage", $nowpage);
        $this->display();
    }
/**
*   添加/修改景点主题
*/
    public function editTheme(){
        $id = I("id");
        if(!empty($id)){
            $data = M("theme")->where("id=".$id)->select();
            $data = $data[0];
            $this->assign("data", $data);
        }
        $this->display();
    }
/**
*   景点主题编辑修改处理程序
*/
    public function doEditTheme(){
        $type = M("theme");
        $data = $type->create();
        if(empty($data["id"])){
            $res = $type->add();
        }
        else{
            $res = $type->save();
        }
        if($res){
            $this->success("操作成功", U("Senic/theme"));
        }
        else{
            $this->error("操作失败", U("Senic/editTheme"));
        }
    }
/*
*   删除景点
*/
    public function delTheme(){
        $id = I("id");
        $res = M("theme")->where("id=".$id)->delete();
        if($res){
            $this->success("操作成功", U("Senic/theme"));
        }
        else{
            $this->error("操作失败", U("Senic/theme"));
        }
    }

// ----------------分--------割--------线--------小--------盆--------友----------------

/**
*   地区联动
*/
    public function area(){
        $id = I("id");
        if(!empty($id)||$id==0){
            $area = M("area");
            $res = $area->where("reid=".$id)->select();
        }
        else{
            $res = array("empty");
        }
        echo json_encode($res);
    }
}